package easy.spring.boot.consul.retry.config;


import com.ecwid.consul.v1.ConsulClient;
import easy.spring.boot.consul.ConsulRetryRegistry;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.consul.discovery.ConsulDiscoveryClient;
import org.springframework.cloud.consul.serviceregistry.ConsulAutoRegistration;
import org.springframework.cloud.consul.serviceregistry.ConsulServiceRegistry;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

/**
 * consul重试自动配置
 */
@Configuration
@ConditionalOnClass({
        ConsulDiscoveryClient.class,
        ConsulAutoRegistration.class,
        ConsulServiceRegistry.class
})
@ConditionalOnProperty(name = "spring.cloud.consul.discovery.register", havingValue = "true", matchIfMissing = true)
@Import({ConsulRetryProperties.class})
@Slf4j
public class ConsulRetryAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean({ConsulRetryRegistry.class})
    public ConsulRetryRegistry consulRetryRegistry(
            ConsulAutoRegistration consulAutoRegistration,
            ConsulServiceRegistry consulServiceRegistry,
            ConsulRetryProperties properties,
            ConsulClient client
    ) {
        return new ConsulRetryRegistry(
                consulAutoRegistration,
                consulServiceRegistry,
                properties,
                client
        );
    }
}
